var express = require('express');
var router = express.Router();

// mysql instance
var mysql = require('mysql');

router.get('/', function(req, res) {
	var cb = function(req, res, resultSet) {
		res.render('mysql-demo', {
			title : "Demo table",
			result : resultSet
		});
	}

	var resultSet = mysqldemoquery(mysql, req, res, cb);

});

function mysqldemoquery(mysql, req, res, cb) {
	// mysql connection information
	var rs;

	var cb2 = function(conn, req, res, cb, rs) {
		return function(err, rows, fields) {
			if (err) {
				rs = "connection error"
				throw err;
			} else {
				rs = rows;
			}
			console.log('The solution is: ', rows);
			conn.end();
			cb(req, res, rs);

		};
	}

	var conn = mysql.createConnection({
		host : '192.168.0.226',
		user : 'root',
		password : 'wdsys2',
		database : 'jw',
		port : 3307
	});
	conn.connect();
	conn.query('SELECT * FROM jw.demo;', cb2(conn, req, res, cb, rs));

	console.log("oooo" + rs);
	return rs;
}

module.exports = router;
